<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            //使用Proxy实现一个对象代理，如果是creatDoM的key的话创建对立的元素，
            //如果它的值是一个数字的话X2返回
            //用interator实现
            let target = {
                name: "target",
            };
            let proxy = new Proxy(target, {
                set(trapTarget, key, value, receiver) {
                    //忽略不希望受到影响已有属性
                    if (!trapTarget.hasOwnProperty(key)) {
                        if (isNaN(value)) {
                            throw new TypeError("属性必须是数字");
                        }
                    }
                    //添加一个属性
                    return Reflect.set(trapTarget, key, value, receiver);
                },
            });
            //添加一个新属性
            proxy.count = 1;
            console.log(proxy.count);
            console.log(target.count);
            proxy.name = "proxy";
            console.log(proxy.name);
            console.log(target.name);
            proxy.anotherName = "proxy";
        </script>
    </body>
</html>
